package com.dreamsail.core.service;

import com.dreamsail.core.dao.MenuDao;
import com.dreamsail.core.dao.RoleDao;
import com.dreamsail.core.dto.MenuDto;
import com.dreamsail.core.frame.ContentManager;
import com.dreamsail.core.model.system.HeadMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单处理类
 */
@Service
public class MenuService {
    @Autowired
    MenuDao menuDao;

    public List<MenuDto> getMenu() {
        HeadMessage headMessage = ContentManager.getHeadMessage();
        List<MenuDto> menuDtos = menuDao.getMenu(headMessage.getUserId());
        Map<Long,List<MenuDto>> map = new HashMap<>();
        for (MenuDto menuDto : menuDtos) {
            Long parentId = menuDto.getParentId();
            if(parentId == null){
                parentId = 0l;
            }
            if(!map.containsKey(parentId)){
                map.put(parentId,new ArrayList<>());
            }
            map.get(parentId).add(menuDto);
        }
        for (MenuDto menuDto : menuDtos) {
           if(map.containsKey(menuDto.getId())){
               menuDto.setChildren(map.get(menuDto.getId()));
           }
        }
        return map.get(0l);
    }
}
